home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
8206
/
8206.xpi
/
content
/
options.js
< prev
next >
Wrap
Text File
|
2010-02-02
|
47KB
|
1,488 lines
var WiseStampOptions = function ()
{
var pub = {};
// public consts
pub.PREFS = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.wisestamp.");
pub.BUNDLE = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService).createBundle("chrome://wisestamp/locale/wisestamp.properties");
// public vars
pub.type = null;
pub.data =
{
_startType: null,
type: null,
sigs:
{}
};
// private vars
var DEFAULT_TYPE = "special-default";
var settings =
{
signatures: [],
autoinsert_compose: true,
autoinsert_thread: true,
hidebuttons: false,
hidestatusbar: false,
promote: true
}
var config_sig = [
{
id: "signature_types_popup",
prefix: "type_",
stopEle: "signature_types_separater"
},
{
id: "manage_sig_box",
prefix: "type_box_",
stopEle: "manage_new_container"
}]
var userLoggedIn = false;
var userEmail = null;
var userPassword = null;
var userEmail = null;
var userPassword = null;
var SUPPORT_MULTI_SIG = false;
var SUPPORT_PREMIUM = WiseStampPrefs.getBoolPref("premium",false);
var editorMode = "wysiwyg";
// public functions
pub.getTypeShortName = function (aType)
{
return encodeURIComponent(aType.replace(/^\s+|\s+$/, "").toLowerCase());
};
function updatePremiumSupport()
{
WiseStampUtils.log("options.js :: updatePremiumSupport :: supported = " + SUPPORT_PREMIUM);
document.getElementById("signature_types_separater").hidden = !SUPPORT_PREMIUM;
document.getElementById("signature_types_premium").hidden = !SUPPORT_PREMIUM;
document.getElementById("signature_types_create").hidden = !SUPPORT_PREMIUM;
document.getElementById("createSignaturePremium").hidden = !SUPPORT_PREMIUM;
if (!SUPPORT_PREMIUM)
{
var prefwin = document.getElementById("BrowserPreferences");
prefwin.removeChild(document.getElementById("paneSecurity"));
prefwin.removeChild(document.getElementById("paneApplications"));
prefwin.removeChild(document.getElementById("paneContent"));
}
}
function updateMultipleSignaturesSupport(supported)
{
WiseStampUtils.log("[options.js :: updateMultipleSignaturesSupport] supported = " + supported);
document.getElementById("createSignatureFromDlg").hidden = !supported;
document.getElementById("createSignaturePremium").hidden = !SUPPORT_PREMIUM || supported;
var deleteLabels = document.getElementById(config_sig[1].id).getElementsByTagName("label");
WiseStampUtils.log("[options.js :: updateMultipleSignaturesSupport] deleteLabels.length = " + deleteLabels.length);
for (var i = 0; i < deleteLabels.length; i++)
deleteLabels[i].hidden = !supported;
document.getElementById("manage_sig_scroll").setAttribute("style","overflow-y: "+(supported ? "scroll" : "auto") + "; overflow-x: hidden;");
document.getElementById("signature_types_create").hidden = !supported;
document.getElementById("signature_types_premium").hidden = !SUPPORT_PREMIUM || supported;
WiseStampUtils.log("[options.js :: updateMultipleSignaturesSupport] <<<<<");
}
pub.onLoad = function (event)
{
WiseStampUtils.log("[options.js :: onLoad] >>>>> ");
WisestampGetAppObject().updateUI();
loadData();
var editor = document.getElementById("signature-wysiwyg");
loadSignaturesList(0);
if (pub.data.type == "none")
pub.setCurrentSignature("personal", true);
else
pub.setCurrentSignature(pub.data.type, true);
pub.loginInBackground();
WiseStampComm.writeLog(WiseStampComm.LOG_EVENT_SETTINGS);
updatePremiumSupport();
if (WiseStampUtils.getDebugMode() == false)
{
document.getElementById("BrowserPreferences").removeChild(document.getElementById("panePrivacy"));
document.getElementById("BrowserPreferences").removeChild(document.getElementById("paneSubmitBug"));
}
WiseStampUtils.log("[options.js :: onLoad] <<<<<");
}
pub.createNewSigFromMenu = function (event)
{
WiseStampUtils.log("options.js :: createNewSigFromMenu :: >>>>>");
var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
var check = { value: null };
var input = { value: "" }; // default the edit field to Bob
var result = prompts.prompt(window, pub.BUNDLE.GetStringFromName("newsigTitle"), pub.BUNDLE.GetStringFromName("newsig"), input, null, check);
var success = false;
if (result && input.value != "")
success = addNewSignatureFromMenu(input.value, event);
event.preventDefault();
event.stopPropagation();
/* select last signature */
var container = document.getElementById("signature_types_popup");
var toSelect = container.childNodes.length - 3;
if (toSelect >= 0)
container.parentNode.selectedIndex = toSelect;
if (success)
pub.setCurrentSignature(getTypeShortName(input.value), true);
}
pub.loadSettings = function ()
{
document.getElementById("autoinsert_compose").checked = WiseStampPrefs.getBoolPref("autoinsert_compose", true, "autoinsert");
document.getElementById("autoinsert_thread").checked = WiseStampPrefs.getBoolPref("autoinsert_thread", true, "autoinsert");
document.getElementById("hidebuttons").checked = pub.PREFS.getBoolPref("hidebuttons");
document.getElementById("hidestatusbar").checked = pub.PREFS.getBoolPref("hidestatusbar");
document.getElementById("promoteChk").checked = pub.PREFS.getBoolPref("promote");
settings.signatures = WiseStampUtils.getSignaturesFromPref();
pub.showHideHelpus();
setTimeout(function () { loadSignaturesList(1); }, 0);
}
pub.validateRss = function (status)
{
if (status != "refreshed")
{
document.getElementById("rss-valid").value = "Validating...";
document.getElementById("rss-valid").setAttribute("status", "");
var url = document.getElementById("rss-url").value;
feedBlog = new WiseStampFeedWrapper(new WiseStampRSSFeed(url), new WiseStampPostFormatter());
//valid_timer = window.setTimeout(function () { WiseStampOptions.validateRss("refreshed"); }, 5000);
feedBlog.refreshCache(function () {
setTimeout(function () { WiseStampOptions.validateRss("refreshed"); }, 0);
});
//window.setTimeout(function () { throw new Error("[debug] " + "1 - " + feedBlog._feed.url); }, 0);
} else
{
//window.setTimeout(function () { throw new Error("[debug] " + "3 - " + feedBlog); }, 0);
//window.setTimeout(function () { throw new Error("[debug] " + "4 - " + feedBlog.format()); }, 0);
if (feedBlog.format()) {
document.getElementById("rss-valid").value = "OK";
document.getElementById("rss-valid").setAttribute("status", "ok");
} else {
document.getElementById("rss-valid").value = "Couldn't validate the feed. Please check the link.";
document.getElementById("rss-valid").setAttribute("status", "error");
}
}
}
pub.setCurrentAsDefault = function ()
{
pub.data.type = pub.type;
pub.PREFS.setCharPref("type", pub.data.type);
var setdefault = document.getElementById("setasdefault");
var isdefault = document.getElementById("isdefault");
setdefault.hidden = true;
isdefault.hidden = false;
}
pub.setCurrentSignature = function (aType, uninit)
{
WiseStampUtils.log("[options.js :: setCurrentSignature] aType = " + aType);
if (aType == "")
return;
if (pub.type && uninit)
storeData(pub.type);
pub.type = aType;
initGeneral();
initEditor();
initIM();
initServices();
WiseStampUtils.log("[options.js :: setCurrentSignature] <<<<<");
}
pub.toggleAdvanced = function (mode)
{
var show = document.getElementById("advanced-show");
var hide = document.getElementById("advanced-hide");
if (mode == "html")
{
show.hidden = false;
hide.hidden = true;
document.getElementById("signature-html-deck").selectedIndex = 0;
} else if (mode == "advanced")
{
show.hidden = true;
hide.hidden = false;
document.getElementById("signature-html-deck").selectedIndex = 1;
}
}
pub.toggleEditor = function (event)
{
var container = document.getElementById("editor-container");
var mode = container.getAttribute("mode");
if (mode == "wysiwyg")
{ //show plain html
editorMode = "html";
container.setAttribute("mode", editorMode);
//event.target.setAttribute("label","&showwysiwyg;");
//event.target.label = "&showwysiwyg;";
document.getElementById("editor-toggle-wysiwyg").hidden = false;
document.getElementById("editor-toggle-html").hidden = true;
var wysiwyg = document.getElementById("signature-wysiwyg");
var text = wysiwyg.contentDocument.body.innerHTML.replace(/<br\/?>+$/, "");
var editor = document.getElementById("signature-html");
//setSourceEditorValue(text,editor);
editor.value = text;
pub.toggleAdvanced("html");
document.getElementById("editor-deck").selectedIndex = 1; //Panel = editor;
document.getElementById("editor-toolbar-deck").selectedPanel = document.getElementById("editor-toolbar-replacement");
} else if (mode == "html") //show what you see is what you get editor
{
editorMode = "wysiwyg";
container.setAttribute("mode", editorMode);
//event.target.setAttribute("label","&showhtml;");
//event.target.label = "showhtml";
document.getElementById("editor-toggle-wysiwyg").hidden = true;
document.getElementById("editor-toggle-html").hidden = false;
var html = document.getElementById("signature-html");
var text = html.value.replace(/(\n|\s)+$/gm, "");
//var text = getSourceEditorValue();
var editor = document.getElementById("signature-wysiwyg");
editor.contentDocument.body.innerHTML = text;
document.getElementById("editor-deck").selectedIndex = 0; //Panel = editor;
document.getElementById("editor-toolbar-deck").selectedPanel = document.getElementById("editor-toolbar-container");
}
}
pub.addNewService = function ()
{
var container = document.getElementById("social-services");
var services = pub.PREFS.getCharPref("services").split(",");
var loaded = container.getAttribute("value").split(",");
for (var i = 0; i < services.length; i++)
{
var sid = services[i];
if (loaded.indexOf(sid) == -1 || i == services.length - 1)
{
var menuitem = document.createElement("wisestamp-service");
menuitem.setAttribute("service", sid);
if (!pub.data.sigs[pub.type].services[sid])
pub.data.sigs[pub.type].services[sid] = [];
var val = pub.data.sigs[pub.type].services[sid][pub.data.sigs[pub.type].services[sid].length];
if (val)
menuitem.setAttribute("value", val);
/* the last child is the add more label */
container.appendChild(menuitem);
container.setAttribute("value", container.getAttribute("value") + "," + sid);
var scroll = document.getElementById("socialservices-scroll");
scroll.boxObject.QueryInterface(Components.interfaces.nsIScrollBoxObject).ensureElementIsVisible(menuitem);
return;
}
}
}
pub.addNewIM = function ()
{
var container = document.getElementById("im-box");
var services = pub.PREFS.getCharPref("im").split(",");
var loaded = container.getAttribute("value").split(",");
for (var i = 0; i < services.length; i++)
{
var sid = services[i];
if (loaded.indexOf(sid) == -1 || i == services.length - 1)
{
var menuitem = document.createElement("wisestamp-im");
menuitem.setAttribute("service", sid);
var val = pub.data.sigs[pub.type].im[sid][pub.data.sigs[pub.type].im[sid].length];
if (val)
menuitem.setAttribute("value", val);
menuitem.setAttribute("width", "170");
/* the last child is the add more label */
container.appendChild(menuitem);
container.setAttribute("value", container.getAttribute("value") + "," + sid);
var scroll = document.getElementById("imservices-scroll");
scroll.boxObject.QueryInterface(Components.interfaces.nsIScrollBoxObject).ensureElementIsVisible(menuitem);
return;
}
}
}
pub.previewSignature = function ()
{
storeData(pub.type);
storeSettings();
openDialog("chrome://wisestamp/content/preview.xul","wisestamp:preview","chrome,centerscreen,resizable,width=550,height=250",{data: pub.data.sigs[pub.type], settings: settings});
}
function applySettings()
{
if (updateRenamedSigs() == false)
return false;
storeData(pub.type);
pub.PREFS.setCharPref("type", pub.data.type);
for (var sig in pub.data.sigs)
{
saveSignatureToConfig(sig);
}
saveSettings();
}
pub.onOK = function ()
{
try
{
var focused = document.commandDispatcher.focusedElement;
var focused_id = focused.parentNode.parentNode.id;
if (focused_id == "email_edit" || focused_id == "password_edit")
{
this.onLogin();
return false;
}
} catch (e) {}
applySettings();
saveSignaturesToServer();
WisestampGetAppObject().saveSignature(pub.data.type);
document.getElementById("BrowserPreferences").setAttribute("lastSelected","paneMain");
this.onClose();
return true;
}
pub.onClose = function ()
{
document.getElementById("BrowserPreferences").setAttribute("lastSelected","paneMain");
return true;
}
pub.deleteSignature = function (aType, removeAll)
{
WiseStampUtils.log("options.js :: deleteSignature :: deleting signature '" + aType + "'");
if (!removeAll)
{
/* Don't let them remove the last signature */
if (settings.signatures.length == 1)
{
WiseStampUtils.alert("deletesignatureerr", "deletesignaturetitle");
return;
}
var confirmed = WiseStampUtils.confirm("deletesignaturetitle", "deletesignatureconfirm");
if (confirmed == 1)
return;
}
var ltype = getTypeShortName(aType);
/* remove signature from menulist dropdown popup */
var container = document.getElementById("signature_types_popup");
var nodes = container.childNodes;
var len = nodes.length;
for (var i = 0; i < len; i++)
{
if (nodes[i].getAttribute("value") == ltype)
{
/* remove node */
container.removeChild(nodes[i]);
break;
}
}
/* remove signature from groupbox in settings pane */
container = document.getElementById("manage_sig_box");
nodes = container.childNodes;
len = nodes.length;
for (var i = 0; i < len; i++)
{
try
{
var nval = getTypeShortName(nodes[i].getAttribute("value"));
if (nval == ltype)
{
/* remove node */
container.removeChild(nodes[i]);
break;
}
} catch(e) {}
/*a text element... If we used a box this wouldn't happen, but we need the div to wrap the elements like we want */
}
/* remove from sig list */
for (var i = 0; i < settings.signatures.length; i++)
{
if (getTypeShortName(settings.signatures[i]) == ltype)
break;
}
settings.signatures.splice(i, 1);
if (removeAll == false) // if some left
{
if (pub.type == ltype)
{
/* We are removing the current signature, reload the next sig */
var newtype = getTypeShortName(settings.signatures[settings.signatures.length - 1]);
pub.data.sigs[newtype];
pub.type = null; //Prevent the current type from being saved
pub.setCurrentSignature(newtype, true);
}
if (ltype == pub.data.type)
{
/*If it is the default, set a new default sig */
pub.type = getTypeShortName(settings.signatures[0]);
pub.setCurrentAsDefault();
}
}
//WiseStampUtils.setSignaturesToPref(settings.signatures);
/* remove old signature from preferences */
WiseStampPrefs.clearBranch(ltype);
pub.data.sigs[ltype] = null;
delete pub.data.sigs[ltype];
WiseStampUtils.log("options.js :: deleteSignature <<<<");
}
pub.showHideHelpus = function ()
{
var promote = document.getElementById("promoteChk").checked;
if (!promote)
document.getElementById("helpusDeck").selectedIndex = 1;
else
document.getElementById("helpusDeck").selectedIndex = 0;
}
// private functions
function getTypeShortName(aType)
{
return encodeURIComponent(aType.replace(/^\s+|\s+$/, "").toLowerCase());
}
function loadSignaturesList(aMode)
{
var container = document.getElementById(config_sig[aMode].id);
//Erase existing items
while ((container.firstChild instanceof XULElement && container.firstChild.getAttribute && container.firstChild.getAttribute("id")!=config_sig[aMode].stopEle) ||
!(container instanceof Element))
{
container.removeChild(container.firstChild);
}
var signatures = WiseStampUtils.getSignaturesFromPref();
signatures.forEach(function (item) { addNewSignatureElement(aMode, item); }, this);
updateMultipleSignaturesSupport(SUPPORT_MULTI_SIG);
}
function addNewSignatureElement(aMode, item)
{
WiseStampUtils.log("options.js :: addNewSignatureElement >>>");
if (!item && item == "")
return;
var ele;
container = document.getElementById(config_sig[aMode].id);
if (!container)
container = document.getElementById(config_sig[aMode].id);
if (aMode == 0) {
/* menulist dropdown box */
ele = document.createElement("menuitem");
ele.setAttribute("label", item);
ele.setAttribute("value", getTypeShortName(item));
ele.setAttribute("id", config_sig[aMode].prefix + getTypeShortName(item));
container.insertBefore(ele, document.getElementById("signature_types_separater"));
} else if (aMode == 1) /* groupbox */
{
ele = document.createElement("hbox");
ele.setAttribute("value",item);
var txt = document.createElement("textbox");
txt.setAttribute("value",item);
txt.setAttribute("width","120")
var deleteLbl = document.createElement("label");
deleteLbl.setAttribute("value", "Delete");
deleteLbl.setAttribute("class", "text-link");
deleteLbl.setAttribute("onclick", "WiseStampOptions.deleteSignature('" + item + "',false)");
ele.appendChild(txt);
ele.appendChild(deleteLbl);
container.insertBefore(ele,document.getElementById("manage_new_container"));
var scroll = document.getElementById("manage_sig_scroll");
scroll.boxObject.QueryInterface(Components.interfaces.nsIScrollBoxObject).ensureElementIsVisible(txt);
var x = {value: null}, y = {value: null};
scroll.boxObject.QueryInterface(Components.interfaces.nsIScrollBoxObject).getScrolledSize(x,y);
scroll.boxObject.QueryInterface(Components.interfaces.nsIScrollBoxObject).scrollTo(x.value,y.value);
}
}
function addNewSignatureFromMenu(value, event)
{
WiseStampUtils.log("options.js :: addNewSignatureFromMenu :: >>>>>");
value = value.replace(/^\s+|\s+$/, "");
var lvalue = encodeURIComponent(value.toLowerCase());
if (!lvalue || lvalue == "")
return false;
var exists = settings.signatures.some(function (sig) { return encodeURIComponent(sig.toLowerCase()) == lvalue; });
if (!exists)
{
addNewSignatureElement(1, value);
addNewSignatureElement(0, value);
pub.data.sigs[lvalue] = loadSignatureFromConfig(DEFAULT_TYPE);
/* load default settings */
saveSignatureToConfig(lvalue);
settings.signatures.push(value);
return true;
} else
WiseStampUtils.alert("errorsigexists");
return false;
}
// Process to delete: Get small type, If no signatures left, don't delete, Remove elements from menulist and manage list, If current,
// select next signature, Remove preferences for that signatures
function storeSettings()
{
settings.autoinsert_compose = document.getElementById("autoinsert_compose").checked;
settings.autoinsert_thread = document.getElementById("autoinsert_thread").checked;
settings.hidebuttons = document.getElementById("hidebuttons").checked;
settings.hidestatusbar = document.getElementById("hidestatusbar").checked;
settings.promote = document.getElementById("promoteChk").checked;
}
function saveSettings()
{
storeSettings();
pub.PREFS.setBoolPref("autoinsert_compose", settings.autoinsert_compose);
pub.PREFS.setBoolPref("autoinsert_thread", settings.autoinsert_thread);
pub.PREFS.setBoolPref("hidebuttons", settings.hidebuttons);
pub.PREFS.setBoolPref("hidestatusbar", settings.hidestatusbar);
pub.PREFS.setBoolPref("promote", settings.promote);
WiseStampUtils.setSignaturesToPref(settings.signatures);
}
function loadData()
{
pub.data._startType = pub.data.type = pub.PREFS.getCharPref("type");
var types = WiseStampUtils.getSignaturesFromPref().map(function (sig) {
return encodeURIComponent(sig.toLowerCase());
});
for each(var t in types) {
try {
pub.data.sigs[t] = loadSignatureFromConfig(t)
} catch(e) {
//Load default
pub.data.sigs[t] = loadSignatureFromConfig(DEFAULT_TYPE);
}
}
}
function loadSignatureFromConfig(t)
{
WiseStampUtils.log("[options.js::loadSignatureFromConfig] type = " + t + " >>>>");
var tData = {};
tData.show = {}
tData.show.services = pub.PREFS.getCharPref(t + ".services");
tData.show.im = pub.PREFS.getCharPref(t + ".im");
tData.im = {}
tData.services = {};
var ims = pub.PREFS.getCharPref("im").split(",");
for (var i = 0; i < ims.length; i++) {
var im = ims[i];
var value = "";
if (pub.PREFS.prefHasUserValue(t + "." + im)) {
value = pub.PREFS.getCharPref(t + "." + im);
if (!value || value == "undefined") value = "";
}
tData.im[im] = value.split("\r\n");
if (!tData.im[im]) tData.im[im] = [];
}
var ids = pub.PREFS.getCharPref("services").split(",");
for (var i = 0; i < ids.length; i++) {
var sid = ids[i];
var value = "";
if (pub.PREFS.prefHasUserValue(t + "." + sid)) {
value = pub.PREFS.getCharPref(t + "." + sid);
if (!value || value == "undefined") value = "";
}
tData.services[sid] = value.split("\r\n");
if (!tData.services[sid]) tData.services[sid] = [];
}
tData.dir = pub.PREFS.getCharPref(t + ".dir");
tData.text = pub.PREFS.getComplexValue(t + ".text", Components.interfaces.nsISupportsString).data;
tData.advanced = pub.PREFS.getComplexValue(t + ".advanced", Components.interfaces.nsISupportsString).data;
tData.rsstitle = pub.PREFS.getComplexValue(t + ".rss-title", Components.interfaces.nsISupportsString).data;
tData.rssurl = pub.PREFS.getComplexValue(t + ".rss-url", Components.interfaces.nsISupportsString).data;
tData.imFormat = {};
tData.servicesFormat = {};
try {
tData.imFormat.hideTitle = WiseStampPrefs.PREFS.getBoolPref(t + ".im.hideTitle");
tData.imFormat.title = WiseStampPrefs.PREFS.getComplexValue(t + ".im.title", Components.interfaces.nsISupportsString).data;
tData.imFormat.showLabels = WiseStampPrefs.PREFS.getBoolPref(t + ".im.showLabels");
tData.imFormat.showIcons = WiseStampPrefs.PREFS.getBoolPref(t + ".im.showIcons");
tData.servicesFormat.hideTitle = WiseStampPrefs.PREFS.getBoolPref(t + ".services.hideTitle");
tData.servicesFormat.title = WiseStampPrefs.PREFS.getComplexValue(t + ".services.title", Components.interfaces.nsISupportsString).data;
tData.servicesFormat.showLabels = WiseStampPrefs.PREFS.getBoolPref(t + ".services.showLabels");
tData.servicesFormat.showIcons = WiseStampPrefs.PREFS.getBoolPref(t + ".services.showIcons");
} catch (e) // if no new fields - take defaults from the old
{
tData.imFormat.hideTitle = WiseStampPrefs.getBoolPref("hidechat",false);
tData.imFormat.title = WiseStampPrefs.getComplexPref("chat", pub.BUNDLE.GetStringFromName("extensions.wisestamp.chat"));
tData.imFormat.showLabels = WiseStampPrefs.getBoolPref("im.text",true);
tData.imFormat.showIcons = WiseStampPrefs.getBoolPref("im.icons",true);
tData.servicesFormat.hideTitle = WiseStampPrefs.getBoolPref("hidecontactme",false);
tData.servicesFormat.title = WiseStampPrefs.getComplexPref("contactme", pub.BUNDLE.GetStringFromName("extensions.wisestamp.contactme"));
tData.servicesFormat.showLabels = WiseStampPrefs.getBoolPref("services.text",false);
tData.servicesFormat.showIcons = WiseStampPrefs.getBoolPref("services.icons",true);
}
WiseStampUtils.log("[options.js::loadSignatureFromConfig] tData = " + WiseStampUtils.serialize(tData));
return tData;
}
function storeData(aType)
{
var editor = document.getElementById("signature-" + editorMode);
if (editorMode == "wysiwyg")
pub.data.sigs[aType].text = editor.contentDocument.body.innerHTML.replace(/<br\/?>+$/, "");
else
pub.data.sigs[aType].text = editor.value.replace(/(\n|\s)+$/gm, "");
pub.data.sigs[aType].advanced = document.getElementById("signature-advanced").value.replace(/(\n|\s)+$/gm, "");
pub.data.sigs[aType].dir = document.getElementById("signature-wysiwyg").contentDocument.dir;
pub.data.sigs[aType].rsstitle = document.getElementById("rss-title").value;
pub.data.sigs[aType].rssurl = document.getElementById("rss-url").value;
var eles = document.getElementById("im-box").getElementsByTagName("wisestamp-im");
var ims = pub.data.sigs[aType].im;
var showIM = [];
for (var i = 0; i < eles.length; i++)
{
ims[eles[i].service] = [];
showIM.push(eles[i].service);
}
for (var i = 0; i < eles.length; i++)
{
ims[eles[i].service].push(eles[i].value);
}
//pub.data.sigs[aType].im = ims;
//updateServicesFromDetails();
var eles = document.getElementById("social-services").getElementsByTagName("wisestamp-service");
var services = pub.data.sigs[aType].services; //{};
var showServices = [];
for (var i = 0; i < eles.length; i++)
{
services[eles[i].service] = [];
showServices.push(eles[i].service);
}
for (var i = 0; i < eles.length; i++)
{
services[eles[i].service].push(eles[i].value);
}
//pub.data.sigs[aType].services = services;
pub.data.sigs[aType].show.services = showServices.join(",");
pub.data.sigs[aType].show.im = showIM.join(",");
}
function initGeneral()
{
try {
var label = document.getElementById(config_sig[0].prefix + pub.type).label;
}
catch (e)
{
alert("Internal Error :: Invalid type - " + pub.type);
return;
}
//Sasha var curSig = document.getElementById("current_signature");
//Sasha curSig.value = pub.BUNDLE.GetStringFromName("currentSignatureMsg").replace("%s",label);
var setdefault = document.getElementById("setasdefault");
var isdefault = document.getElementById("isdefault");
if (pub.type == pub.data.type)
{
setdefault.hidden = true;
isdefault.hidden = false;
} else
{
setdefault.hidden = false;
isdefault.hidden = true;
}
document.getElementById("rss-title").value = pub.data.sigs[pub.type].rsstitle;
document.getElementById("rss-url").value = pub.data.sigs[pub.type].rssurl;
document.getElementById("rss-valid").value = "Validate";
document.getElementById("rss-valid").setAttribute("status", "ready");
document.getElementById("signature_types_popup").parentNode.value = pub.type;
var container = document.getElementById("signature_types_popup");
var nodes = container.childNodes
var len = nodes.length
for (var i = 0; i < len; i++)
{
if (nodes[i].getAttribute("value") == pub.type)
{
/* remove node */
container.parentNode.selectedIndex = i;
break;
}
}
}
function initEditor()
{
/* temporary workaround */
var editor = document.getElementById("signature-wysiwyg");
var html = pub.data.sigs[pub.type].text;
WiseStampUtils.log("[options.js::initEditor] HTML = " + html);
WiseStampInitWYSIWYGEditor(editor, html, pub.data.sigs[pub.type].dir);
editor = document.getElementById("signature-html");
//initSourceEditor(editor,html);
editor.value = html;
document.getElementById("signature-advanced").value = pub.data.sigs[pub.type].advanced
}
function initIM()
{
var ims = pub.PREFS.getCharPref(pub.type + ".im"),
counts = {};
if (ims.length > 0)
{
var box = document.getElementById("im-box");
box.setAttribute("value", pub.data.sigs[pub.type].show.im);
ims = ims.split(",");
while (box.firstChild)
box.removeChild(box.firstChild);
for (var i = 0; i < ims.length; i++)
{
var im = ims[i];
if (im in counts) counts[im]++;
else counts[im] = 0;
var num = counts[im];
if (im && im.length > 0)
{
var item = document.createElement("wisestamp-im");
item.setAttribute("service", im);
var val = pub.data.sigs[pub.type].im[im][num];
if (val) item.setAttribute("value", val);
item.setAttribute("width", "170");
/* insert before label */
box.appendChild(item);
}
}
}
}
function initServices() {
var services = pub.PREFS.getCharPref(pub.type + ".services"),
counts = {};
if (services.length > 0)
{
services = services.split(",");
var container = document.getElementById("social-services");
container.setAttribute("value", services.join(","));
while (container.firstChild)
container.removeChild(container.firstChild);
for (var i = 0; i < services.length; i++)
{
var sid = services[i];
if (sid in counts) counts[sid]++;
else counts[sid] = 0;
var num = counts[sid];
var item = document.createElement("wisestamp-service");
item.setAttribute("service", sid);
var val = pub.data.sigs[pub.type].services[sid][num];
if (val) item.setAttribute("value", val);
/* insert before label */
container.appendChild(item);
}
}
}
function updateRenamedSigs()
{
var settings_container = document.getElementById(config_sig[1].id);
var menu_container = document.getElementById(config_sig[0].id);
var count = -1;
var sigsCount = settings_container.childNodes.length - 1;
for (var iSig = 0; iSig < sigsCount; iSig++)
{
var ele = settings_container.childNodes[iSig];
if (!ele.getAttribute)
continue;
var txt = ele.getElementsByTagName("textbox")[0];
if (txt.value == "")
{
WiseStampUtils.log("options.js :: updateRenamedSigs :: " + pub.BUNDLE.GetStringFromName("empty_sig_name_error"));
WiseStampUtils.alert("empty_sig_name_error");
return false;
}
}
for (var i = 0; i < sigsCount; i++)
{
var ele = settings_container.childNodes[i];
if (!ele.getAttribute)
continue;
count++;
var origval = ele.getAttribute("value");
var txt = ele.getElementsByTagName("textbox")[0];
// signature was renamed
if (origval != txt.value)
{
if (signatureExists(txt.value))
{
var error = WisestampSignatureFactory.strings.GetStringFromName("duplicate_signatures");
error = error.replace("%s",txt.value);
WiseStampUtils.alert2(error);
return false;
}
var oldshort = getTypeShortName(origval);
var newshort = getTypeShortName(txt.value);
ele.setAttribute("value", txt.value);
pub.data.sigs[newshort] = loadSignatureFromConfig(DEFAULT_TYPE);
pub.data.sigs[newshort] = pub.data.sigs[oldshort];
saveSignatureToConfig(newshort);
delete pub.data.sigs[oldshort];
loadSignatureFromConfig(newshort);
settings.signatures[count] = txt.value;
var ele2 = menu_container.childNodes[count];
ele2.setAttribute("label", txt.value);
ele2.setAttribute("value", getTypeShortName(txt.value));
ele2.setAttribute("id", config_sig[0].prefix + getTypeShortName(txt.value));
ele.setAttribute("id", config_sig[1].prefix + getTypeShortName(txt.value));
if (oldshort == pub.data.type) /*If it is the default, set as new default sig */
pub.data.type = newshort;
if (pub.type == oldshort) /* We are removing the current signature, reload the next sig */
pub.setCurrentSignature(newshort, false);
/* remove old signature from preferences */
WiseStampPrefs.clearBranch(oldshort);
}
}
return true;
}
function saveSignatureToConfig(aType)
{
WiseStampUtils.log("options.js :: saveSignatureToConfig >>>");
var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
str.data = pub.data.sigs[aType].text;
pub.PREFS.setComplexValue(aType + ".text", Components.interfaces.nsISupportsString, str);
str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
str.data = pub.data.sigs[aType].advanced;
pub.PREFS.setComplexValue(aType + ".advanced", Components.interfaces.nsISupportsString, str);
str.data = pub.data.sigs[aType].rsstitle;
pub.PREFS.setComplexValue(aType + ".rss-title", Components.interfaces.nsISupportsString, str);
str.data = pub.data.sigs[aType].rssurl;
pub.PREFS.setComplexValue(aType + ".rss-url", Components.interfaces.nsISupportsString, str);
pub.PREFS.setCharPref(aType + ".im", pub.data.sigs[aType].show.im);
for (var im in pub.data.sigs[aType].im)
pub.PREFS.setCharPref(aType + "." + im, pub.data.sigs[aType].im[im].join("\r\n"));
for (var service in pub.data.sigs[aType].services)
pub.PREFS.setCharPref(aType + "." + service, pub.data.sigs[aType].services[service].join("\r\n"));
pub.PREFS.setCharPref(aType + ".services", pub.data.sigs[aType].show.services);
pub.PREFS.setCharPref(aType + ".dir", pub.data.sigs[aType].dir);
pub.PREFS.setBoolPref(aType + ".services.hideTitle", pub.data.sigs[aType].servicesFormat.hideTitle);
WiseStampPrefs.setComplexPref(aType + ".services.title", pub.data.sigs[aType].servicesFormat.title);
pub.PREFS.setBoolPref(aType + ".services.showLabels", pub.data.sigs[aType].servicesFormat.showLabels);
pub.PREFS.setBoolPref(aType + ".services.showIcons", pub.data.sigs[aType].servicesFormat.showIcons);
pub.PREFS.setBoolPref(aType + ".im.hideTitle", pub.data.sigs[aType].imFormat.hideTitle);
WiseStampPrefs.setComplexPref(aType + ".im.title", pub.data.sigs[aType].imFormat.title);
pub.PREFS.setBoolPref(aType + ".im.showLabels", pub.data.sigs[aType].imFormat.showLabels);
pub.PREFS.setBoolPref(aType + ".im.showIcons", pub.data.sigs[aType].imFormat.showIcons);
}
///////////////////////////////////////////////////////////////////////////
function updateAccountStatus()
{
var user_suffix = "";
if (userLoggedIn)
{
if (document.getElementById("account_login") != null)
{
document.getElementById("account_login").setAttribute("disabled", "true");
document.getElementById("email_edit").setAttribute("disabled", "true");
document.getElementById("password_edit").setAttribute("disabled", "true");
}
user_suffix = " :: " + userEmail;
} else
{
document.getElementById("account_login").removeAttribute("disabled");
document.getElementById("email_edit").removeAttribute("disabled");
document.getElementById("password_edit").removeAttribute("disabled");
userEmail = null;
userPassword = null;
WiseStampUtils.userCode = null;
}
/*var prefwin = document.getElementById("BrowserPreferences");
var title = prefwin.getAttribute('title');
title = title + user_suffix;
alert(title);
prefwin.setAttribute('title',title);*/
}
pub.onLoginResponse = function (xml, email, password)
{
if (xml == null)
{
WiseStampUtils.alert("comm_action_failed");
return false;
}
if (xml.nodes.result.text != "1")
{
if (xml.nodes.error != undefined && xml.nodes.error.text.length > 0)
WiseStampUtils.alert2(xml.nodes.error.text);
else
WiseStampUtils.alert("signatures_saved");
return false;
}
userLoggedIn = true;
pub.PREFS.setCharPref("user_email", email);
WiseStampUtils.storePassword(email,password);
userEmail = email;
userPassword = password;
WiseStampUtils.setUserCode(xml.nodes.data.nodes.code.text);
updateAccountStatus();
syncSignaturesWithServer();
SUPPORT_MULTI_SIG = (xml.nodes.data.nodes.plan.text == "corporate");
updateMultipleSignaturesSupport(SUPPORT_MULTI_SIG);
}
function login(email, password, async)
{
WiseStampUtils.log("options.js :: login >>> email/password = " + email + "/" + password);
if (email == "")
return;
userLoggedIn = false;
pub.PREFS.setBoolPref("logged_in", true); // mark that login should be attempted on start-up
WiseStampComm.login(email, password, function(xml) { pub.onLoginResponse(xml,email,password); },async);
}
pub.onLogin = function ()
{
var email = document.getElementById("email_edit").value;
var password = document.getElementById("password_edit").value;
if (email == "" || password == "")
{
WiseStampUtils.alert("login_parameters_missing");
return;
}
login(email, password, false);
}
pub.onLogout = function ()
{
userLoggedIn = false;
updateAccountStatus();
WiseStampPrefs.PREFS.setBoolPref("logged_in", false);
WiseStampPrefs.PREFS.setBoolPref("synced", false);
SUPPORT_MULTI_SIG = false;
updateMultipleSignaturesSupport(SUPPORT_MULTI_SIG);
}
pub.loginInBackground = function ()
{
WiseStampUtils.log("[options.js :: loginInBackground] >>>>>");
if (!SUPPORT_PREMIUM)
return;
var logged_in = WiseStampPrefs.getBoolPref("logged_in", false);
var email = WiseStampPrefs.getCharPref("user_email", "");
var password = WiseStampUtils.getPassword(email);
WiseStampUtils.log("options.js :: loginInBackground :: logged_in = " + logged_in + ", email/password = " + email + "/" + password);
var email_edit = document.getElementById("email_edit");
var password_edit = document.getElementById("password_edit");
if (email_edit != null) // may be executed from overlay
{
document.getElementById("email_edit").value = email;
document.getElementById("password_edit").value = password;
}
if (logged_in)
login(email,password,true);
}
pub.onRegister = function ()
{
var email = document.getElementById("email_edit").value;
var password = document.getElementById("password_edit").value;
var args = { email: email, password: password, result: null };
var dlgURL = "chrome://wisestamp/content/registerDlg.xul";
openDialog(dlgURL, "", "chrome,titlebar,toolbar,centerscreen,modal", args);
if (args.result != true)
return;
pub.onLogout();
document.getElementById("email_edit").value = args.email;
document.getElementById("password_edit").value = args.password;
login(args.email, args.password, false);
}
function saveSignaturesToServer()
{
WiseStampUtils.log("options.js :: saveSignaturesToServer :: logged_in = " + userLoggedIn);
if (!userLoggedIn)
return;
var signatures = [];
for (var i = 0; i < settings.signatures.length; i++)
{
var typeName = settings.signatures[i];
var type = getTypeShortName(typeName);
var data = WiseStampUtils.serialize(pub.data.sigs[type]);
// ampresand screws up the post data - fix it
data = data.replace(/&/g,"%26");
signatures[i] = {data: data, type: typeName};
}
//signature = escape(signature);
var xml = WiseStampComm.saveSignatures(userEmail, userPassword, signatures);
if (xml == null)
{
WiseStampUtils.alert("comm_action_failed");
return false;
}
return true;
}
function signatureExists(typeName)
{
var type = getTypeShortName(typeName);
return pub.data.sigs.hasOwnProperty(type);
}
function checkIfSignaturesChanged(signatures)
{
var sigs_changed = false;
if (signatures.nodeCount != settings.signatures.length)
sigs_changed = true;
else
{
for (var i = 1; i <= signatures.nodeCount; i++)
{
var typeName = signatures.nodes['item_' + i].nodes.type.text;
var type = getTypeShortName(typeName);
var exists = signatureExists(typeName);
if (!exists ||
WiseStampUtils.serialize(pub.data.sigs[type]) != signatures.nodes['item_' + i].nodes.data.text)
{
WiseStampUtils.log("options.js :: checkIfSignaturesChanged :: signature " + typeName + " was changed");
sigs_changed = true;
break;
}
}
}
return sigs_changed;
}
function syncSignaturesWithServer()
{
if (!userLoggedIn)
return;
var xml = WiseStampComm.loadSignatures(userEmail,userPassword);
if (xml == null)
{
WiseStampUtils.alert("action_failed");
return;
}
var signatures = xml.nodes.data.nodes.signatures;
WiseStampUtils.log("options.js :: syncSignaturesWithServer :: " + settings.signatures.length + " local, " + signatures.nodeCount + " on server");
if (signatures.nodeCount == 0)
{
// todo: check for failure
if (saveSignaturesToServer() == true)
WiseStampUtils.alert("first_login_message");
return;
}
var synced_already = WiseStampPrefs.getBoolPref("synced",false);
if (synced_already)
{
loadSignaturesFromServer(signatures);
return;
}
var question_result = null;
for (var i = 1; i <= signatures.nodeCount; i++)
{
var typeName = signatures.nodes['item_'+i].nodes.type.text;
var type = getTypeShortName(typeName);
var signature_data = signatures.nodes['item_'+i].nodes.data.text;
var exists = pub.data.sigs.hasOwnProperty(type);
if (!exists || WiseStampUtils.serialize(pub.data.sigs[type]) != signature_data)
{
WiseStampUtils.log("options.js :: syncSignaturesWithServer :: signature " + type + " was changed");
if (question_result == null)
{
question_result = WiseStampUtils.confirm(
'application_name','signature_sync_question',
'signature_sync_question_button_server',
'signature_sync_question_button_local',
'signature_sync_question_button_merge');
if (question_result == 0)
{
clearLocalSignatures();
loadSignaturesFromServer(signatures,true);
break;
}
if (question_result == 1)
{
saveSignaturesToServer();
break;
}
}
// if signature with the same name already exists - change the name
var newTypeName = typeName;
if (exists)
{
var iSignature = 1;
while (signatureExists(typeName + ' ('+iSignature+')'))
iSignature++;
newTypeName = typeName + ' ('+iSignature+')';
}
loadSignatureFromServer(newTypeName,signature_data);
}
}
saveSignaturesToServer();
WiseStampPrefs.PREFS.setBoolPref("synced",true);
}
function loadSignatureFromServer(typeName,signature)
{
WiseStampUtils.log("options.js :: loadSignatureFromServer :: loading signature '" + typeName + "'");
pub.deleteSignature(typeName,true);
addNewSignatureElement(1, typeName);
addNewSignatureElement(0, typeName);
var type = getTypeShortName(typeName);
pub.data.sigs[type] = signature;
saveSignatureToConfig(type);
settings.signatures.push(typeName);
}
function clearLocalSignatures()
{
for (var i = settings.signatures.length-1; i >= 0; i--)
pub.deleteSignature(settings.signatures[i],true);
}
function loadSignaturesFromServer(signatures,changed)
{
WiseStampUtils.log("[options.js::loadSignaturesFromServer] >>>");
if (changed != true)
changed = checkIfSignaturesChanged(signatures);
if (changed == false)
return;
for (var i = 1; i <= signatures.nodeCount; i++)
{
var typeName = signatures.nodes['item_'+i].nodes.type.text;
var data = signatures.nodes['item_'+i].nodes.data.text;
WiseStampUtils.log("[options.js::loadSignaturesFromServer] name = " + typeName + ", data = " + data);
var signature = WiseStampUtils.parseSerialized(data);
loadSignatureFromServer(typeName,signature);
}
/*If it is the default, set a new default sig */
var defaultStillExists = pub.data.sigs.hasOwnProperty(pub.data.type);
if (!defaultStillExists)
pub.data.type = getTypeShortName(settings.signatures[0]);
pub.setCurrentSignature(pub.data.type,false);
//pub.setCurrentAsDefault();
// SASHA1 applySettings();
WiseStampUtils.alert('signatures_loaded');
}
pub.onLoadSignatures = function()
{
if (userLoggedIn == false)
{
WiseStampUtils.alert("login_required","login_required_title");
return;
}
var xml = WiseStampComm.loadSignatures(userEmail,userPassword);
if (xml == null)
{
WiseStampUtils.alert("comm_action_failed");
return;
}
var signatures = xml.nodes.data.nodes.signatures;
clearLocalSignatures();
loadSignaturesFromServer(signatures);
}
pub.onSaveSignatures = function()
{
if (userLoggedIn == false)
{
WiseStampUtils.alert("login_required","login_required_title");
return;
}
if (saveSignaturesToServer() == false)
return false;
WiseStampUtils.alert("signatures_saved");
return true;
}
pub.onSocialFormatDlg = function()
{
var args = { settings: pub.data.sigs[pub.type].servicesFormat, result: null };
var dlgURL = "chrome://wisestamp/content/dialogs/socialFormatDlg.xul";
openDialog(dlgURL, "", "chrome,titlebar,toolbar,centerscreen,modal", args);
if (args.result != true)
return;
pub.data.sigs[pub.type].servicesFormat = args.settings;
}
pub.onImFormatDlg = function()
{
var args = { settings: pub.data.sigs[pub.type].imFormat, result: null };
var dlgURL = "chrome://wisestamp/content/dialogs/imFormatDlg.xul";
openDialog(dlgURL, "", "chrome,titlebar,toolbar,centerscreen,modal", args);
if (args.result != true)
return;
pub.data.sigs[pub.type].imFormat = args.settings;
}
////////////////////////////////////////////////////////////////////////////
pub.onResetSettings = function()
{
if (!confirm("You are about to reset all configuration. Are you sure?"))
return;
WiseStampPrefs.clearBranch("");
}
pub.onExportSettings = function()
{
var file = WiseStampUtils.browseFile(false,"WiseStamp backup file","wst");
if (file == null)
return;
WiseStampPrefs.export(file);
}
pub.onImportSettings = function()
{
var file = WiseStampUtils.browseFile(true,"WiseStamp backup file","wst");
if (file == null)
return;
WiseStampPrefs.import(file);
}
pub.onExportLog = function()
{
var file = WiseStampUtils.browseFile(false,"WiseStamp log file","wlg");
if (file == null)
return;
WiseStampUtils.exportLog(file);
}
function runTest(timeout)
{
if (pub.runningLoadTest == false)
{
document.getElementById('loadTestButton').setAttribute('label','Run load test...');
return;
}
WiseStampComm.writeLog(WiseStampComm.LOG_EVENT_SIG_INSERT,"test_"+WiseStampUtils.getUserCode());
setTimeout(function(){runTest(timeout);},timeout);
document.getElementById('loadTestButton').setAttribute('label','Test running (click to stop). #' + pub.loadTestCount);
pub.loadTestCount++;
}
pub.onRunLoadTest = function(i)
{
if (pub.runningLoadTest == true)
{
pub.runningLoadTest = false;
return;
}
var args = {
title: "Define load test",
intro: "",
items: {test_timeout: {type: "textbox", label: "Timeout between tests (ms)"}}
};
var dlgURL = "chrome://wisestamp/content/dialogs/formDlg.xul";
openDialog(dlgURL, "", "chrome,titlebar,toolbar,centerscreen,modal", args);
if (args.result != true)
return;
pub.runningLoadTest = true;
pub.loadTestCount = 0;
runTest(args.items.test_timeout.value);
}
pub.showPremiumInfo = function(event)
{
document.getElementById("BrowserPreferences").showPane(document.getElementById("paneContent"));
}
return pub;
} ();